package com.hopu.oa.controller.plan;

import com.hopu.oa.entity.plan.AoaPlanList;
import com.hopu.oa.entity.user.AoaUser;

import com.hopu.oa.service.impl.plan.MyPlanServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @author Secret
 * @create 2021-07-19 下午 01:01
 */
@Controller
public class MyPlanController {
    @Autowired
    private MyPlanServiceImpl myPlanService;


    @RequestMapping("myplan")
    public String myplan(@RequestParam(value = "page", defaultValue = "1") int page,
                         @RequestParam(value = "size", defaultValue = "5") int size,
                         @RequestParam(value = "choose", defaultValue = "1") int choose,
                         HttpSession session, Model model,
                         @Param("starttime") String startTime,
                         @Param("endtime") String endTime,
                         @Param("userName") String userName) {

        AoaUser user = (AoaUser) session.getAttribute("user");

        int totalElements = myPlanService.queryMyPlan(user.getUserId(), page, 0, choose, startTime, endTime, userName).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaPlanList> umap = myPlanService.queryMyPlan(user.getUserId(), (page - 1) * size, size, choose, startTime, endTime, userName);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("umap", umap);
        model.addAttribute("url", "realplantable");
        System.out.println(umap);
        return "plan/plantable";
    }

    @RequestMapping("realplantable")
    public String realplantable(@RequestParam(value = "page", defaultValue = "1") int page,
                                @RequestParam(value = "size", defaultValue = "5") int size,
                                @RequestParam(value = "choose", defaultValue = "1") int choose,
                                HttpSession session, Model model,
                                @RequestParam(value = "starttime", defaultValue = "") String startTime,
                                @RequestParam(value = "endtime", defaultValue = "") String endTime,
                                @Param("userName") String userName) {
        AoaUser user = (AoaUser) session.getAttribute("user");

        int totalElements = myPlanService.queryMyPlan(user.getUserId(), page, 0, choose, startTime, endTime, userName).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaPlanList> umap = myPlanService.queryMyPlan(user.getUserId(), (page - 1) * size, size, choose, startTime, endTime, userName);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("umap", umap);
        model.addAttribute("url", "realplantable");

        return "plan/realplantable";
    }


    @RequestMapping("semyplan")
    public String semyplan(@RequestParam(value = "page", defaultValue = "1") int page,
                           @RequestParam(value = "size", defaultValue = "5") int size,
                           @RequestParam(value = "choose", defaultValue = "1") int choose,
                           HttpSession session, Model model,
                           @RequestParam("startTime") String startTime,
                           @RequestParam("endTime") String endTime,
                           @Param("userName") String userName) {
        AoaUser user = (AoaUser) session.getAttribute("user");

        int totalElements = myPlanService.queryMyPlan(user.getUserId(), page, 0, choose, startTime, endTime, userName).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaPlanList> umap = myPlanService.queryMyPlan(user.getUserId(), (page - 1) * size, size, choose, startTime, endTime, userName);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);

        model.addAttribute("page", map);
        model.addAttribute("umap", umap);
        model.addAttribute("url", "semyplan");

        return "plan/realplantable";
    }

    @RequestMapping("updatecomment")
    public String updatecomment(@Param("pid") int pid, @Param("comment") String comment) {
        String s = myPlanService.selectCommentById(pid);
        String ss = s.length() > 0 ? s : "";
        String comm = ss + comment;
        myPlanService.updatePlanComment(pid, comm);
        return "redirect:myplan";
    }
}
